define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'aps/erpbomlog/index',
                    add_url: 'aps/erpbomlog/add',
                    edit_url: 'aps/erpbomlog/edit',
                    del_url: 'aps/erpbomlog/del',
                    multi_url: 'aps/erpbomlog/multi',
                    table: 'aps_erpbomlog',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{field: 'workorder_id', title: __('Workorder_id')},
                        //{field: 'md5', title: __('Md5')},
                        {field: 'workorder.name', title: __('Workorder.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'planlog.state', title: __('工單狀態'), defaultValue:__('未完成'), searchList: {"未完成":__('未完成'),"已完成":__('已完成')}, formatter: Table.api.formatter.normal, sortable: true},
                        {field: 'yuqi', title: __('Yuqi'), searchList: {"未逾期":__('未逾期'),"逾期":__('逾期')}, formatter:Controller.api.formatter.yqstatus},
                        {field: 'workorder.MainProcessName', title: __('Workorder.mainprocessname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'productnumber', title: __('Productnumber'), operate:false},
                        {field: 'productname', title: __('Productname'), operate:false},
                        {field: 'workorder.num', title: __('預計產量'), operate:false},
                        {field: 'zhidingjiean', title: __('來料到齊狀態'), searchList: {"0":__('已開請購單'),"1":__('有库存'),"2":__('待进料'),"3":__('已验收'),"4":__('待验收')}, formatter: Table.api.formatter.normal},
                        {field: 'demanddate', title: __('Demanddate'), operate:false},
                        {field: 'requisitiondate', title: __('Requisitiondate'), operate:false},
                        {field: 'requisitionquantity', title: __('Requisitionquantity'), operate:false},

                        {field: 'purchasedate', title: __('Purchasedate'), operate:false},

                        {field: 'purchasequantity', title: __('Purchasequantity'), operate:false},

                        {field: 'yujiaori', title: __('Yujiaori'), operate:false, formatter:Controller.api.formatter.yqstatus},

                        {field: 'jinhuoshuliang', title: __('Jinhuoshuliang'), operate:false},
                        {field: 'acceptancedate', title: __('Acceptancedate'), operate:false},
                        {field: 'rejectedquantity', title: __('Rejectedquantity'), operate:false},
                        {field: 'batchnumber', title: __('Batchnumber'), operate:false},
                        {field: 'inspectionhours', title: __('Inspectionhours'), operate:false},

                        //{field: 'create_time', title: __('Create_time'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        ////{field: 'workorder.id', title: __('Workorder.id')},
                        //{field: 'workorder.order_id', title: __('Workorder.order_id')},
                        //{field: 'workorder.number', title: __('Workorder.number')},
                        //{field: 'workorder.startdate', title: __('Workorder.startdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'workorder.finishdate', title: __('Workorder.finishdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'workorder.num', title: __('Workorder.num')},
                        //{field: 'workorder.mainprocess_id', title: __('Workorder.mainprocess_id')},
                        //{field: 'workorder.subprocess_id', title: __('Workorder.subprocess_id')},
                        //{field: 'workorder.procedure1_id', title: __('Workorder.procedure1_id')},
                        //{field: 'workorder.procedure2_id', title: __('Workorder.procedure2_id')},
                        //{field: 'workorder.procedure3_id', title: __('Workorder.procedure3_id')},
                        //{field: 'workorder.procedure4_id', title: __('Workorder.procedure4_id')},
                        //{field: 'workorder.procedure5_id', title: __('Workorder.procedure5_id')},
                        //{field: 'workorder.procedure6_id', title: __('Workorder.procedure6_id')},
                        //{field: 'workorder.procedure7_id', title: __('Workorder.procedure7_id')},
                        //{field: 'workorder.procedure8_id', title: __('Workorder.procedure8_id')},
                        //{field: 'workorder.procedure9_id', title: __('Workorder.procedure9_id')},
                        //{field: 'workorder.procedure10_id', title: __('Workorder.procedure10_id')},
                        //{field: 'workorder.procedure11_id', title: __('Workorder.procedure11_id')},
                        //{field: 'workorder.procedure12_id', title: __('Workorder.procedure12_id')},
                        //{field: 'workorder.procedure1_manhours', title: __('Workorder.procedure1_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure2_manhours', title: __('Workorder.procedure2_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure3_manhours', title: __('Workorder.procedure3_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure4_manhours', title: __('Workorder.procedure4_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure5_manhours', title: __('Workorder.procedure5_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure6_manhours', title: __('Workorder.procedure6_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure7_manhours', title: __('Workorder.procedure7_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure8_manhours', title: __('Workorder.procedure8_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure9_manhours', title: __('Workorder.procedure9_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure10_manhours', title: __('Workorder.procedure10_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure11_manhours', title: __('Workorder.procedure11_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.procedure12_manhours', title: __('Workorder.procedure12_manhours'), operate:'BETWEEN'},
                        //{field: 'workorder.update_time', title: __('Workorder.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'workorder.create_time', title: __('Workorder.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'workorder.ProductNumber', title: __('Workorder.productnumber')},
                        //{field: 'workorder.ProductName', title: __('Workorder.productname')},
                        //
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_aps_erpbomlog', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_aps_erpbomlog')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_aps_erpbomlog')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                yqstatus: function (value, row, index) {
                    if (row.yujiaori > row.demanddate) {
                        return [
                            '<span class="text-danger">' + value + '</span>'
                        ].join("")

                    } else {
                        return [
                            '<span class="text-success">' + value + '</span>'
                        ].join("")
                    }
                },
            }
        }
    };
    return Controller;
});